home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 9 / CDACTUAL9.iso / share / Dos / VARIOS / pascal / SWAG9605.DDD / 0035_BCD Add & Subtract functions.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-05-31  |  1.8 KB  |  51 lines

  1. {
  2. Depending on the application, BCD may solve your problem.  But if you
  3. really need a _large_ binary integer you are going to have to use
  4. multiple precision arithmetic.  This is relatively easy to do in assembler
  5. and just a little more difficult in a high-level language.
  6.  
  7. You need to define your _integer_ as an array of 256 bytes, 128 words,
  8. or 64 unsigned long integers.  So we're stuck with words.  So our bigint
  9. is an array[0..127] of word;  We'll do it little-endian 0=least
  10. significant word, 127 = most significant word.  Using words with a longint
  11. intermediate value actually makes our task a little easier.
  12. }
  13.  
  14. CONST MaxBIG  = 127;
  15. TYPE  tBigInt = Array[0..MaxBIG] of Word;
  16.  
  17. PROCEDURE BigAdd(VAR Op1, Op2: tBigInt);
  18. { --------------------------------------------- }
  19. { Do multiprecision add:  Op1 := Op1 + Op2      }
  20. { --------------------------------------------- }
  21. VAR i: Integer;
  22.     Temp: Longint;
  23. Begin
  24.     Temp := 0;                       { Clear carry }
  25.     For i := 0 to MaxBIG Do Begin
  26.        Temp   := Longint(Op1[i]) + Op2[i] + Temp;
  27.        Op1[i] := Word(Temp);
  28.        Temp   := Temp shr 16;  { Carry = High word }
  29.     End;
  30. END;
  31.  
  32. PROCEDURE BigSub(VAR Op1, Op2: tBigInt);
  33. { --------------------------------------------- }
  34. { Do multiprecision Substract: Op1 := Op1 - Op2 }
  35. { --------------------------------------------- }
  36. VAR i: Integer;
  37.     Temp: Longint;
  38. Begin
  39.     Temp := 0;                       { Clear carry }
  40.     For i := 0 to MaxBIG Do Begin
  41.        Temp   := Longint(Op1[i]) - Op2[i] - Temp;
  42.        Op1[i] := Word(Temp);
  43.        Temp   := Temp shr 16;  { Carry = High word }
  44.     End;
  45. END;
  46.  
  47. I've done the easy part.  It's your turn to put together
  48. the multiprecision multiply and divide.  If Op2 can be
  49. an integer I'll toss together an op1*Op2 and Op1 div Op2.
  50. But for a full version I'd have to crack the books :-)
  51.